package com.jhy.new2025.BinaryTree;


import java.util.ArrayDeque;

public class Connect1 {
    public Node1 connect(Node1 root) {
        if (root == null) return null;
        ArrayDeque<Node1> deque = new ArrayDeque<>();
        deque.offer(root);
        // 外层循环
        while (!deque.isEmpty()) {
            // 定义一个前驱节点在前面
            Node1 pre = null;
            int size = deque.size();
            while (size > 0) {
                Node1 cur = deque.poll();
                if (pre != null) {
                    cur = pre.next;
                }
                pre = cur;
                if(cur.left!=null) deque.offer(cur.left);
                if(cur.right!=null) deque.offer(cur.right);
                size--;
            }
            if (pre != null) {
                pre.next = null;
            }
        }
        return null;
    }

}
